<template>
    <div style="width: 100%">
        <div id="deawer">
            <v-tableTitle :btnConfig="btnConfig" :tableSelectData="tableSelectData" @cancel="cancel"
                          @del="del" @save="save"></v-tableTitle>
            <div class="detailListBox">
                <v-detailList ref="detailList" @setSelectData="setSelectData"></v-detailList>
            </div>
        </div>
    </div>

</template>

<script>
    import baseMixin from "@/mixins/BaseMixin";
    import skipMixin from "@/mixins/SkipMixin";
    import config from "../Config";
    import detailList from "../detailsList/DetailList"
    import tableTitle from "commons/TableTitle";

    export default {
        name: "",
        mixins: [baseMixin, skipMixin, config],
        data() {
            return {
                newFlag: false,
                tableSelectData: [],
                routeData: {},
            };
        },
        created() {
        },
        computed: {
            btnConfig() {
                if (this.newFlag) {
                    return [
                        {label: this.$t('common.save'), certigier: this.saveStatus, invalid: "save"},
                        {label: this.$t('common.return'), invalid: "cancel"},
                    ]
                } else {
                    return [
                        {label: this.$t('common.delete'), certigier: this.actionStatus, disabled: true, invalid: "del"},
                        {label: this.$t('common.return'), invalid: "cancel"},
                    ]
                }

            }
        },
        methods: {
            async save() {
                let data = this.$refs.detailList.detailData;
                try {
                    let res = await this.$api.post(`/saveReplenish`, data);
                    
                        this.$message.success(res.returnMgr);
                        this.cancel();
                    
                } catch (e) {
                    this.$message.error(e);
                }
            },
            async del(data) {
                try {
                    let res = await this.$api.post(`/deleteReplenishDetail`, data);
                    
                        this.$message.success(res.returnMgr);
                        this.$refs.detailList.$children[0].getTableData(1, {});
                        this.$refs.detailList.$children[0].$refs.table.delSelectData(data);
                    
                } catch (e) {
                    this.$message.error(e);
                }
            },
            //弹框取消
            cancel() {
                this.$router.push({name: "qtyreplenish"});
            },
            setSelectData(data) {
                this.tableSelectData = data;
            }
        },
        mounted() {
            this.newFlag = this.$store.state.newFlag;
            if (Object.keys(this.$store.state.skip).length !== 0) {
                let data = this.$store.state.skip;
                this.routeData = data.routeData;
                this.newFlag = data.newFlag;
            } else {
                this.routeData = this.$store.state.routeData;
            }

        },
        watch: {
            tableSelectData(data) {
                if (this.permissionStatus) {
                    if (data.length > 0) {
                        this.actionStatus = false;
                    } else {
                        this.actionStatus = true;
                    }
                }
            }
        },
        components: {
            "v-detailList": detailList,
            "v-tableTitle": tableTitle,
        }
    }
</script>

<style scoped>

</style>
